Opt("MouseCoordMode", 0) ;Window
Opt("PixelCoordMode", 0)

Opt("GUIOnEventMode", 1)

#include <Date.au3>
#include <Timers.au3>
#include <FileConstants.au3>
#include <ColorConstants.au3>
#include <ProgressConstants.au3>

; Global Defaults
Const $UpdateSpeed = 50

Const $MouseClickVariance = 10

Const $MinutesToMilli = 60000

Const $TimerTolerance = 1000

Global $IsPaused = True

Local $ActiveWindowHandle = 0

; Script Variables
Const $GoldfishX[] = [1040, 797, 563, 874, 762, 1007, 750, 528]
Const $GoldfishY[] = [453, 380, 488, 515, 382, 455, 434, 491]
Const $GoldfishCount = 8

Const $MonsterX = 860
Const $MonsterY = 415

Const $PurchaseHeroX = 100
Const $PurchaseHeroY = 400

Const $FarmModeButtonX = 1120
Const $FarmModeButtonY = 280

Const $CooldownReduction = 75
Const $BaseSpellTimer = 10
Const $ReducedSpellTimer = $BaseSpellTimer * ((100 - $CooldownReduction)/100)

Global $TimerDelta = 0

Local $GoldfishTimerHandle = TimerInit()
Local $AutoLevelTimerHandle = TimerInit()

Global $TimeSinceLastGoldfish = 0
Global $TimeSinceLastAutoLevel = 0

Global $ProgressionMode = True

#cs
Opt ('WinTitleMatchMode','2') ; Helps With Getting The Window Information...
$Title = 'Clicker Heroes' ; The Name Of The Game...
$Full = WinGetTitle ($Title) ; Get The Full Title..
$HWnD = WinGetHandle ($Full) ; Get The Handle
$iButton = 'left' ; Button The Mouse Will Click I.E. "Left Or Right"
$iClicks = '1' ; The Number Of Times To Click
$iX = '$MonsterX + RandVariance()' ; The "X" Pos For The Mouse To Click
$iY = '$MonsterY + RandVariance()' ; The "Y" Pos For The Mouse To Click
#ce

HotKeySet("^{ENTER}", "end")
HotKeySet("^{`}", "ActivateWindow")
HotKeySet("^{1}", "PauseResume")
HotKeySet("^{2}", "PurchaseHero")
HotKeySet("^{3}", "FarmMode")
HotKeySet("^{4}", "DPSSpells")
HotKeySet("^{5}", "GoldSpells")

$ApplicationHandle = WinGetHandle("Clicker Heroes", "")

$TimerWindowHandle = GUICreate("Steam Version Timers", 300, 50, 10, 675, -1, -1, $ApplicationHandle)

Global $PauseButton = GUICtrlCreateButton("Paused", 10, 10, 47)
GUICtrlSetOnEvent($PauseButton, "PauseResume")

Global $ProgressionButton = GUICtrlCreateButton("Progression", 65, 10)
GUICtrlSetOnEvent($ProgressionButton, "SwitchMode")

Global $AutoLevelButton = GUICtrlCreateButton("Timer", 140, 10)

GUISetState(@SW_SHOW, $TimerWindowHandle)

ActivateWindow()

While True ;Main Loop

	Sleep($UpdateSpeed)

	UpdateTimersAndGUI()

	If Not $IsPaused Then

		If $TimeSinceLastGoldfish > 5000 Then

			GoldfishClicker()

		EndIf

		If $TimeSinceLastAutoLevel > ($ReducedSpellTimer * $MinutesToMilli) + $TimerTolerance Then

			AutoLevel()

		EndIf

		MonsterClicker()

	EndIf

WEnd

Func PauseResume()

	If $IsPaused Then

		$IsPaused = False
		GUICtrlSetData($PauseButton, "Running")

	Else

		$IsPaused = True
		GUICtrlSetData($PauseButton, "Paused")

	EndIf

	ActivateWindow()

EndFunc   ;==>PauseResume

Func MonsterClicker()

	;If IsHWnD ($HWnD) And WinExists ($Full) <> '0' Then ; Win Check
	;ControlClick ($HWnD, '','', $iButton, $iClicks, $iX, $iY) ; Clicking The Window While Its Minmized
	;EndIf

	MouseClick("left", $MonsterX, $MonsterY, 1, 0)

EndFunc   ;==>MonsterClicker

Func GoldfishClicker()

	For $Iter = 0 To ($GoldfishCount - 1) Step 1

		;If IsHWnD ($HWnD) And WinExists ($Full) <> '0' Then ; Win Check
		;ControlClick ($HWnD, '','', $iButton, $iClicks, $GoldfishX[$Iter], $GoldfishY[$Iter]) ; Clicking The Window While Its Minmized
		;EndIf

		MouseClick("left", $GoldfishX[$Iter], $GoldfishY[$Iter],1,0)

	Next

	$GoldfishTimerHandle = TimerInit()

	$TimeSinceLastGoldfish = 0

EndFunc   ;==>GoldfishClicker

Func AutoLevel()

	ActivateWindow()

	If $ProgressionMode Then
		FarmMode()
	EndIf

	RitualSpells()
	DPSSpells()

	$AutoLevelTimerHandle = TimerInit()

	$TimeSinceLastAutoLevel = 0

EndFunc

Func UpdateTimersAndGUI()

	$TimerDelta = TimerDiff($GoldfishTimerHandle)
	$TimeSinceLastGoldfish += $TimerDelta
	$GoldfishTimerHandle = TimerInit()

	$TimerDelta = TimerDiff($AutoLevelTimerHandle)
	$TimeSinceLastAutoLevel += $TimerDelta
	$AutoLevelTimerHandle = TimerInit()

	If ($TimeSinceLastAutoLevel < $MinutesToMilli) Then

		GUICtrlSetData($AutoLevelButton, "0:" & Int($TimeSinceLastAutoLevel / 1000))

	Else

		GUICtrlSetData($AutoLevelButton, Int($TimeSinceLastAutoLevel / $MinutesToMilli) & ":" & Mod(Int($TimeSinceLastAutoLevel / 1000), 60))

	EndIf

EndFunc

Func PurchaseHero()

	MouseWheel("up", 5)
	MouseWheel("down", 15)

	MouseClick("left", $PurchaseHeroX, $PurchaseHeroY, 1, 1)

EndFunc   ;==>PurchaseHero

Func RitualSpells()

	Send("8")
	Send("6")
	Send("9")

EndFunc

Func DPSSpells()

	Send("2")
	Send("3")
	Send("4")
	Send("5")
	Send("7")
	Send("1")

EndFunc   ;==>DPSSpells

Func GoldSpells()

	;8-5-9-4-1-2-3-7

	Send("8")
	Send("5")
	Send("9")
	Send("4")
	Send("1")
	Send("2")
	Send("3")
	Send("7")

EndFunc   ;==>GoldSpells

Func FarmMode()

	MouseClick("left", $FarmModeButtonX, $FarmModeButtonY, 1, 1)

EndFunc   ;==>FarmMode

Func SwitchMode()

	If $ProgressionMode Then

		$ProgressionMode = False
		GUICtrlSetData($ProgressionButton, "Farming")

	Else

		$ProgressionMode = True
		GUICtrlSetData($ProgressionButton, "Progression")

	EndIf

EndFunc
;;--------------------------- Default Stuffs -------------------------------------------------------

Func RandVariance()

	Return (Random(0, $MouseClickVariance * 2, 1) - $MouseClickVariance)

EndFunc   ;==>RandVariance

Func end()

	GUIDelete($TimerWindowHandle)
	Exit

EndFunc   ;==>end

Func _WinWaitActivate($title, $text, $timeout = 15)
	$ApplicationHandle = WinWait($title, $text, $timeout)
	If Not WinActive($title, $text) Then WinActivate($title, $text)
	WinWaitActive($title, $text, $timeout)
EndFunc   ;==>_WinWaitActivate

Func ActivateWindow()

	_WinWaitActivate("Clicker Heroes", "")

EndFunc   ;==>ActivateWindow

Func GetActiveWindow()

	$ActiveWindowHandle = WinGetHandle("[ACTIVE]")

EndFunc   ;==>GetActiveWindow
